package Solutions

func reverseBetween(head *ListNode, left int, right int) *ListNode {
	var cur *ListNode = &ListNode{
		Next: head,
	}
	head = cur
	for i := 1; ; i++ {
		if left == i {
			var h, t, l = reverse(cur.Next, right, i)
			t.Next = l
			cur.Next = h
			return head.Next
		}
		cur = cur.Next
	}
}

func reverse(head *ListNode, right int, ind int) (*ListNode, *ListNode, *ListNode) {
	if ind < right {
		var h, t, l = reverse(head.Next, right, ind+1)
		t.Next = head
		head.Next = nil
		return h, head, l
	} else if ind == right {
		return head, head, head.Next
	}
	return nil, nil, nil
}
